﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.segments.data_type;

namespace ConsoleApplication1.segments
{
    enum Order_control
    {
        NM,//new order
        CA,//cancel order
        DC,//Discontinue order
        HD,//Hold order
        RL,//Release/cancel previous hold
        XO,//Change/update order
    };
    class ORC
    {
        public StringBuilder Order_Control { get; set ; }
        public StringBuilder placer_order { get; set; }
        public StringBuilder filler_order { get; set; }
        public StringBuilder order_status { get; set; }
        public CM Quantity { get; set; } // rquired woth ORM segments
        public CN entered_by{ get; set; }
        public TS 	Transaction_date_time { get; set; }

        public ORC()
        {
            Order_Control = new StringBuilder();
            placer_order = new StringBuilder();
            filler_order = new StringBuilder();
            order_status = new StringBuilder();
            Quantity = new CM();
            entered_by = new CN();
            Transaction_date_time = new TS();
        }
        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }


        public int fill(string source, int temp)
        {
            int i, x = 0;
            for (i = temp; i < source.Length; i++)
            {

                if (check(source, i) != null)
                {
                    Order_Control.Capacity = Order_Control.Length;
                    placer_order.Capacity = placer_order.Length;
                    filler_order.Capacity = filler_order.Length;
                    order_status.Capacity = order_status.Length;
                    return i - 1;
                }
                else if (source[i] == '|')
                {
                    if (x >= 10)
                    {
                        Order_Control.Capacity = Order_Control.Length;
                        placer_order.Capacity = placer_order.Length;
                        filler_order.Capacity = filler_order.Length;
                        order_status.Capacity = order_status.Length;
                        return i;
                    }
                    else
                    {
                        x++;
                        continue;
                    }
                }
                else
                {
                    if (x == 1)  // passing prameter of set id - PID
                    {
                        if (source[i] != '|')
                        {                           
                            Order_Control.Insert(Order_Control.Length, source[i]);
                        }
                    }
                    else if (x == 2)
                    {

                        if (source[i] != '|')
                        {                          
                            placer_order.Insert(placer_order.Length, source[i]);
                        }
                    }
                    else if (x == 3)
                    {
                        if (source[i] != '|')
                        {
                            if (filler_order == null)
                                filler_order = new StringBuilder();
                            filler_order.Insert(filler_order.Length, source[i]);
                        }
                    }
                    else if (x >= 4 && x < 5)
                    {
                        while (true)
                        {
                            if (source[++i] == '|')
                            {
                                x++;
                                break;
                            }
                        }
                    }
                    else if (x == 5)
                    {
                        if (source[i] != '|')
                        {
                            order_status.Insert(order_status.Length, source[i]);
                        }
                    }
                    else if (x >= 6 && x < 7)
                    {
                        while (true)
                            if (source[++i] == '|')
                            {
                                x++;
                                break;
                            }
                    }
                    else if (x == 7)
                    {
                        if (source[i] != '|')
                        {
                            i = Quantity.fill(source, i);
                        }
                    }
                    else if (x >= 7 && x < 9)
                    {
                        while (true)
                            if (source[++i] == '|')
                            {
                                x++;
                                break;
                            }
                    }
                    else if (x == 9)
                    {
                       
                            i =Transaction_date_time .fill(source, i);
                       
                    }
                    else if (x == 10)
                    {
                        if (source[i] != '|')
                        {

                            i = entered_by.fill(source, i);
                            return i;
                        }
                    }
                }
            }
            return i;
        }

        public override string ToString()
        {
            string orc_string = "ORC";


            if (Order_Control.Length != 0)
                orc_string += "|" + Order_Control;
            if (placer_order.Length != 0)
                orc_string += "|" + placer_order;
            if (filler_order.Length != 0)
                orc_string += "|" + filler_order;
            if (order_status.Length != 0)
                orc_string += "||" + order_status;
            if (Quantity != null)
                orc_string += "||" + Quantity.tostring();
            if (Transaction_date_time != null)
                orc_string += "|" + Transaction_date_time.ToString();
            if (entered_by != null)
                orc_string += "|" + entered_by.ToString();
            

            return orc_string;
        }
    }
}
